*** Table with baseline heterogeneous effects ***

** Preamble **
 
clear all
set more off


local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"


* Note: local directory is the output folder 
cd "`path_output'"


					** Loading the input file **


use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local baseline_controls "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"
local CGB_FEs			"pct_sh_tv1991 pct_sh_rural pct_renda1991"



/*generating percentiles*/
foreach var in `baseline_controls' {
xtile pct_`var'=  `var', n(10)
replace pct_`var'=0 if turno==2
}


/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0


label var treatment "Globo"
label var treatment1 "Globo and Others"
label var treatment2 "Globo Only"


									** Table: henetrogeneous effects **									
																		
										
/*
The table reports regression estimates of the effect of Globo's edited coverage on Lula's and Collor's second-round vote shares according to demographic characteristics. All specifications control for municipality fixed effects, time fixed effects, and first rounds' vote shares interected with time fixed effects. Columns (1) and (3) reports baseline estimates on Lula's vote share. Columns (2) and (4) reports baseline estimates on Collor's vote share. In columns (1) and (2), we control for group fixed effects. To create group fixed effects, we split municipalities into deciles according to the share of households with TV, the share of population living in rural areas, and the income per capita. In columns (3) and (4), untreated units are weighted by inverse propensity scores to create a control group with similar pretreatment characteristics (Abadie, 2005). Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported in brackets. Significantly different from zero at $99\% (***)$, $95\%(**)$ and $ 90\%(*)$ confidence level.
*/

local legend "The table reports regression estimates of the effect of Globo's edited coverage on Lula's and Collor's second-round vote shares according to demographic characteristics. All specifications control for municipality fixed effects, time fixed effects, and first rounds' vote shares interected with time fixed effects. Columns (1) and (3) reports baseline estimates on Lula's vote share. Columns (2) and (4) reports baseline estimates on Collor's vote share. In columns (1) and (2), we control for group fixed effects. To create group fixed effects, we split municipalities into deciles according to the share of households with TV, the share of population living in rural areas, and the income per capita. In columns (3) and (4), untreated units are weighted by inverse propensity scores to create a control group with similar pretreatment characteristics (Abadie, 2005). Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported in brackets. Significantly different from zero at $99\% (***)$, $95\%(**)$ and $ 90\%(*)$ confidence level."


* Set interactions *
																		
foreach var in pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural1991 sh_radio1991 {

	sum `var'
	gen T_`var'=treatment*(`var'-r(mean))

}

label var T_pop1991 	  "Population  ($\times$ 1,000)"
label var T_years_sch1991 "Years of schooling"
label var T_renda1991 	  "Income per capta (in min. wages)"
label var T_sh_tv1991 	  "Share of households with TV"
label var T_sh_agua1991   "Share of households with piped water"
label var T_sh_elect1991  "Share of households with electricity"
label var T_sh_rural1991  "Share of pop. in rural areas"
label var T_sh_radio1991  "Share of households with radio"

foreach v of varlist T_* {

	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
	
}


eststo: xi: reghdfe  sh_Tpt_  treatment T_* turno sh_Tpsp_-sh_Tpsdb_            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
estadd local PartiesControl "Yes"

eststo: xi: reghdfe  sh_Tprn_ treatment T_* turno sh_Tpsp_-sh_Tpsdb_            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
estadd local PartiesControl "Yes"

eststo: xi: reghdfe  sh_Tpt_  treatment T_* turno sh_Tpsp_-sh_Tpsdb_ [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge) 
estadd local PartiesControl "Yes"

eststo: xi: reghdfe  sh_Tprn_ treatment T_* turno sh_Tpsp_-sh_Tpsdb_ [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
estadd local PartiesControl "Yes"


									** Producing table **


esttab est1 est2 est3 est4 using heterogeneidade.tex,                                                    					  ///
keep( treatment T_sh_tv1991 T_pop1991 T_years_sch1991 T_renda1991 T_sh_agua1991 T_sh_elect1991 T_sh_rural1991 T_sh_radio1991) ///
order(treatment T_sh_tv1991 T_pop1991 T_years_sch1991 T_renda1991 T_sh_agua1991 T_sh_elect1991 T_sh_rural1991 T_sh_radio1991) ///
stat(N r2 PartiesControl, fmt(0 2 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{c}{@}")          ///
label("Observations" "R-squared" "Electoral controls * Round"))                                                               ///
mgroups("Group FEs" "Reweighted", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span}))     ///
star(* 0.10 ** 0.05 *** 0.01)                                                                                                 ///
nomtitles r2 b(3) replace  se(3) brac compress nonotes label  nogaps staraux                                                  ///
refcat(T_sh_tv1991 "Globo $\times$" , nolabel) 																				  ///
nonotes addnotes("\begin{minipage}{.9\linewidth} \footnotesize \smallskip \textbf{Note:} `legend' \end{minipage}" )
